//
//  TravelyjANDlxdViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/19.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelyjANDlxdViewController.h"
#import "TravellvxingdiCollectionViewCell.h"
#import "TravelyoujiCollectionViewCell.h"
#import "TravetTourDetailsViewController.h"
#import "EnterDetailViewController.h"
@interface TravelyjANDlxdViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, TravellvxingdiCollectionViewCellDelegate,TravelyoujiCollectionViewCellDelegate>
@property (nonatomic, retain) UICollectionView *mainColleation;

@property (nonatomic, retain) UILabel *label;

@property (nonatomic, retain) UIButton *foreignBtn;

@property (nonatomic, retain) UIButton *domesticBtn;

@property (nonatomic, retain) UIAlertController *alert;
@end

@implementation TravelyjANDlxdViewController

- (void)dealloc
{
    [_name release];
    [_mainColleation release];
    [_label release];
    [_alert release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *viewLabel = [[UIView alloc] initWithFrame:CGRectMake(0 *kWidth, 0 *kHeight, 375 *kWidth, 64 *kHeight)];
    viewLabel.backgroundColor = [UIColor blackColor];
    [self.view addSubview:viewLabel];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10 *kWidth, 25 *kHeight, 25 *kWidth, 25 *kHeight);
    [btn setImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    [viewLabel addSubview:btn];
    UILabel *city = [[UILabel alloc] initWithFrame:CGRectMake(40 *kWidth, 25 *kHeight, 295 *kWidth, 30 *kHeight)];
    city.textColor = [UIColor whiteColor];
    city.textAlignment = NSTextAlignmentCenter;
    city.font = [UIFont systemFontOfSize:22 *kWidth];
    city.text = self.name;
    [viewLabel addSubview:city];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initButton];
    [self initMainCollectionView];
    

}
#pragma init
- (void)initMainCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.itemSize = CGSizeMake(375 *kWidth , 567 *kHeight);
    layout.minimumLineSpacing = 0;
    self.mainColleation = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100 *kWidth , self.view.frame.size.width , 567 *kHeight) collectionViewLayout:layout];
    
    self.mainColleation.dataSource = self;
    
    self.mainColleation.delegate = self;
    
    self.mainColleation.scrollEnabled = YES;
    
    self.mainColleation.pagingEnabled = YES;
    
    
    [self.mainColleation setBackgroundColor:[UIColor colorWithWhite:0.801 alpha:1.000]];
    
    [self.mainColleation registerClass:[TravellvxingdiCollectionViewCell class] forCellWithReuseIdentifier:@"TravellvxingdiCollectionViewCell"];
    
    [self.mainColleation registerClass:[TravelyoujiCollectionViewCell class] forCellWithReuseIdentifier:@"TravelyoujiCollectionViewCell"];
    
    [self.view addSubview:self.mainColleation];
    
    [_mainColleation release];
    
    [layout release];
    
    
}
- (void)initButton{
    
    UIView *viewLabel = [[UIView alloc] initWithFrame:CGRectMake(10 *kWidth, 70 *kHeight, 355 *kWidth, 25 *kHeight)];
    viewLabel.backgroundColor = [UIColor colorWithWhite:0.944 alpha:1.000];
    viewLabel.layer.cornerRadius = 4 *kWidth;
    [self.view addSubview:viewLabel];
    
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(1 *kWidth, 1 *kHeight, 175.5 *kWidth, 23 *kHeight)];
    self.label.layer.cornerRadius = 4 *kWidth;
    self.label.layer.borderWidth = 1;
    self.label.layer.borderColor = [UIColor colorWithRed:0.882 green:0.882 blue:0.902 alpha:2].CGColor;
    self.label.backgroundColor = [UIColor whiteColor];
    [viewLabel addSubview:self.label];
    
    self.foreignBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.foreignBtn.frame = CGRectMake(0, 0, 175.5 *kWidth, 25 *kHeight);
    [self.foreignBtn setTitle:@"游记" forState:UIControlStateNormal];
    [self.foreignBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.foreignBtn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    [viewLabel addSubview:self.foreignBtn];
    [self change:self.foreignBtn];
    self.domesticBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.domesticBtn.frame = CGRectMake(175.5 *kWidth, 0, 175.5 *kWidth, 25 *kHeight);
    [self.domesticBtn setTitle:@"旅行地" forState:UIControlStateNormal];
    [self.domesticBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.8] forState:UIControlStateNormal];
    [self.domesticBtn addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    [viewLabel addSubview:self.domesticBtn];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(0, 100 *kHeight, 375 *kWidth, 1*kHeight)];
    line.backgroundColor = [UIColor colorWithRed:0.882 green:0.882 blue:0.902 alpha:2];
    [self.view addSubview:line];
    
}
#pragma mark Action
- (void)pop{

    [self.navigationController popViewControllerAnimated:YES];

}
- (void)change:(UIButton *)btn{
    
    if (btn == self.domesticBtn) {
        NSLog(@"国内");
        [self.foreignBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.8] forState:UIControlStateNormal];
        [self.domesticBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.label.frame = CGRectMake(177.5*kWidth, 1 *kHeight, 175.5*kWidth, 23 *kHeight);
        }];
        self.mainColleation.contentOffset = CGPointMake(375 *kWidth, 0);
        
    }else{
        NSLog(@"国外");
        [self.foreignBtn setTitleColor:[UIColor orangeColor]  forState:UIControlStateNormal];
        [self.domesticBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.8]forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.label.frame = CGRectMake(1 *kWidth, 1 *kHeight, 176.5 *kWidth, 23 *kHeight);
        }];
        self.mainColleation.contentOffset = CGPointMake(0, 0);
    }
}
- (void)remove{
    
    [self.alert dismissViewControllerAnimated:YES completion:nil];

}
#pragma collectionView代理回调方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

        if (indexPath.row == 0) {
            TravelyoujiCollectionViewCell *cell = [self.mainColleation dequeueReusableCellWithReuseIdentifier:@"TravelyoujiCollectionViewCell" forIndexPath:indexPath];
            cell.searchName = self.name;
            cell.delegate = self;
            return cell;
            
        }else{
            TravellvxingdiCollectionViewCell *cell = [self.mainColleation dequeueReusableCellWithReuseIdentifier:@"TravellvxingdiCollectionViewCell" forIndexPath:indexPath];
            cell.searchName = self.name;
            cell.delegate = self;
            return cell;
        }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    CGFloat f = self.mainColleation.contentOffset.x / 375;
    if (f == 0) {
        [self.foreignBtn setTitleColor:[UIColor orangeColor]  forState:UIControlStateNormal];
        [self.domesticBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.8]forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.label.frame = CGRectMake(1 *kWidth, 1 *kHeight, 176.5 *kWidth, 23 *kHeight);
        }];

    }else if (f == 1){
        
        [self.foreignBtn setTitleColor:[[UIColor grayColor] colorWithAlphaComponent:0.8] forState:UIControlStateNormal];
        [self.domesticBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.label.frame = CGRectMake(177.5*kWidth, 1 *kHeight, 175.5*kWidth, 23 *kHeight);
        }];
        self.mainColleation.contentOffset = CGPointMake(375 *kWidth, 0);
    
    }
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0 *kWidth, 0 *kHeight, 0 *kWidth, 0*kHeight);
    
}
#pragma mark 代理方法
- (void)sender:(NSNumber *)strNumber{

    TravetTourDetailsViewController *travel = [[TravetTourDetailsViewController alloc] init];
    travel.idNumber = strNumber;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:travel animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}
- (void)senderPer{

    self.alert = [UIAlertController alertControllerWithTitle:nil message:@"没有数据!" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:self.alert animated:YES completion:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(remove) userInfo:nil repeats:NO];
}
- (void)send:(EntertainmentModel *)enter{

    EnterDetailViewController *en = [[EnterDetailViewController alloc] init];
    en.enterModel = enter;
    [self.navigationController pushViewController:en animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
